﻿using CPPEI.BD.VMTool.Until;
using CPPEI.WPF.Core;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace CPPEI.BD.VMTool
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// CPPEI.BD.VMTool.exe /mode forward 转发模式启动，点击方案按钮后，将数据发送到agent程序
    /// CPPEI.BD.VMTool.exe 直接启动，点击方案按钮后，将数据发送到视频切换程序
    /// </summary>
    public partial class App : XApplication
    {
        /// <summary>
        /// true: 转发模式启动，点击方案按钮后，将数据发送到agent程序,
        /// false:直接启动，点击方案按钮后，将数据发送到视频切换程序
        /// </summary>
        public static bool ISFORWARDMODE = false;

        /// <summary>
        /// 启动VMTool的展示程序所处的domainid
        /// </summary>
        public static string Coolzon_Domain;
        /// <summary>
        /// 启动VMTool的展示程序所处的方案名称
        /// </summary>
        public static string Coolzon_Solution;
        /// <summary>
        /// 启动VMTool的展示程序所处的场景名称
        /// </summary>
        public static string Coolzon_Scene;

        public static Mutex mut;

        public App()
            : base("CPPEI", "BD", "VMTool", "视频切换工具")
        {
            CPPEI.Coolzon.Common.Logger.InitByLogDirectory(string.Format("{0}\\logs\\", AppDomain.CurrentDomain.BaseDirectory));
        }

        protected override void OnXStartup(Coolzon.Common.Startup.StartupParamCollection startupParams)
        {
            bool requestInitialOwnership = true;
            bool mutexWasCreated;
            mut = new System.Threading.Mutex(requestInitialOwnership, "com.Application1.Ding", out mutexWasCreated);
            if (!(requestInitialOwnership && mutexWasCreated))
            {
                Process process = AppRunOneHelper.RuningInstance();

                AppRunOneHelper.HandleRunningInstance(process);

                Application.Current.Shutdown();
                return;
            }
          
           


            base.OnXStartup(startupParams);

            if (startupParams.Count > 0)
            {
                CPPEI.Coolzon.Common.Startup.StartupParam param = startupParams.Get("mode");
                if (param != null)
                {
                    string mode = param.Value;
                    if (string.Equals(mode, "forward", StringComparison.CurrentCultureIgnoreCase))
                    {
                        ISFORWARDMODE = true;
                    }
                }

                Coolzon_Domain = startupParams.GetValue("domain");
                Coolzon_Solution = startupParams.GetValue("solution");
                Coolzon_Scene = startupParams.GetValue("scene");
            }

            CPPEI.Coolzon.Common.Logger.DebugToTag("App", "ISFORWARDMODE:{0} domain:{1} solution:{2} scene:{3}",
                ISFORWARDMODE,
                Coolzon_Domain,
                Coolzon_Solution,
                Coolzon_Scene);

            StartupUri = new Uri("./VMToolWindow.xaml", UriKind.Relative);
        }
    }
}
